package com.mostromas.ess.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mostromas.ess.entities.ProductCategory;
import com.mostromas.ess.product.mapper.ProductCategoryMapper;
import com.mostromas.ess.product.req.ProductCategoryREQ;
import com.mostromas.ess.product.service.IProductCategoryService;
import com.mostromas.ess.util.base.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements IProductCategoryService {
    @Override
    public Result queryPage(ProductCategoryREQ req) {
        //查询条件对象
        QueryWrapper<ProductCategory> wrapper = new QueryWrapper();

        //如果字段名不为空
        if(StringUtils.isNotEmpty(req.getName())){
            wrapper.like("name",req.getName());
        }
        if(req.getStatus() != null){
            wrapper.eq("status",req.getStatus());
        }
        if(req.getParentId()!= null){
            wrapper.eq("parent_id",req.getParentId());
        }
        //排序
        wrapper.orderByDesc("status").orderByAsc("sort");

        //第一个参数是page分页对象，第二个参数是查询条件
        IPage<ProductCategory> data = baseMapper.selectPage(req.getPage(), wrapper);
        return Result.ok(data);
    }

    @Override
    public boolean updateById(ProductCategory productCategory) {
        //设置更新时间
        productCategory.setUpdateDate(new Date());

        return super.updateById(productCategory);
    }

    @Override
    public Result findAllNormal() {
        //查询条件对象
        QueryWrapper<ProductCategory> wrapper = new QueryWrapper();

        wrapper.eq("status",1);

        List<ProductCategory> categories = baseMapper.selectList(wrapper);
        return Result.ok(categories);
    }

}
